<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" width="60%" @ok="handleSubmit">

    <a-row >
      <a-col span="8" v-if="!isUpdate">
        <a-list item-layout="horizontal" :data-source="cardData" :pagination="pagination">
          <template #renderItem="{ item }">
            <a-list-item  @click="handleClick(item)">
              <a-list-item-meta
                description=""
              >
                <!-- <template #title>
                  <span >{{ item.templateName }}</span>
                </template> -->
              </a-list-item-meta>
              <a-row>
                <a-col span="24">{{'模板名称：'+item.templateName}}</a-col>
                <a-col span="12">{{'投入品：'+item.input_dictText}}</a-col>
                <a-col span="12">{{'用法与用量：'+item.usageAndDosage}}</a-col>
                <a-col span="12">{{'作业类型：'+item.productionOperation_dictText}}</a-col>
                <a-col span="12">{{'作业开始时间：'+item.jobStartTime}}</a-col>
                <a-col span="12">{{'作业周期：'+item.jobDuration}}</a-col>
                <a-col span="12">{{'是否临时安排：'+item.randomJob_dictText}}</a-col>
                <a-col span="12">{{'计划操作人：'+item.sysUser.realname}}</a-col>
                <a-col span="12">{{'种植计划：'+item.growPlanId_dictText}}</a-col>
                <a-col span="12">{{'作业工艺：'+item.jobIntroduce}}</a-col>
                <a-col span="12">{{'作业工艺说明：'+item.operationDescription}}</a-col>

              </a-row>

            </a-list-item>
          </template>
        </a-list>
      </a-col>
      <a-col  :span="isUpdate ? 24 : 16">
        <BasicForm @register="registerForm"/>
      </a-col>
    </a-row>
  </BasicModal>

</template>

<script lang="ts" setup>
    import {ref, computed, unref,nextTick } from 'vue';
    import {BasicModal, useModalInner} from '/@/components/Modal';
    import {BasicForm, useForm} from '/@/components/Form/index';
    import {formSchema} from '../TProductionSchedule.data';
    import {saveOrUpdate,createProductionScheduleByTemplateId} from '../TProductionSchedule.api';
    import {list} from "/@/views/planting-process/productionScheduleTemplate/ProductionScheduleTemplate.api";
    // Emits声明
    const emit = defineEmits(['register','success']);
    const isUpdate = ref(true);

    const cardData = ref([]);
    const pagination = {
      total: 0,
      showQuickJumper:true,
      onChange: (page: number) => {
        pageList({pageSize:5,pageNo:page})
      },
      pageSize: 5,
    };
    //表单配置
    const [registerForm, {setProps,resetFields, setFieldsValue, validate}] = useForm({
        //labelWidth: 150,
        schemas: formSchema,
        showActionButtonGroup: false,
        baseColProps: {span: 24}
    });
    //表单赋值
    const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
      if (data&&data.record) {
        data.record.growPlanId = data.record.growPlanId.toString();
      }
        //重置表单
        await resetFields();
        setModalProps({confirmLoading: false,showCancelBtn:!!data?.showFooter,showOkBtn:!!data?.showFooter});
        isUpdate.value = !!data?.isUpdate;
        if (unref(isUpdate)) {
            //表单赋值
            await setFieldsValue({
                ...data.record,
            });
        }else {
          pageList({pageSize:5,pageNo:1})
        }
        // 隐藏底部时禁用整个表单
       setProps({ disabled: !data?.showFooter })
    });
    //设置标题
    const title = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
    //表单提交事件
    async function handleSubmit(v) {
        try {
            let values = await validate();
            setModalProps({confirmLoading: true});
            //提交表单
            await saveOrUpdate(values, isUpdate.value);
            //关闭弹窗
            closeModal();
            //刷新列表
            emit('success');
        } finally {
            setModalProps({confirmLoading: false});
        }
    }
    async  function pageList(page){
      list(page).then(res => {

        cardData.value = res.records;
        pagination.total = res.total;
      })
    }

    async function handleClick(item) {
      // 这里是你处理点击事件的代码
      createProductionScheduleByTemplateId({templateId:item.id})
        .then(async res => {
          await nextTick();
          await resetFields();
          await setFieldsValue({...res})
        })
    }
</script>

<style lang="less" scoped>
	/** 时间和数字输入框样式 */
  :deep(.ant-input-number){
		width: 100%
	}

	:deep(.ant-calendar-picker){
		width: 100%
	}
</style>
